package leetcode;

import java.util.*;

/**
 * 下一个更大元素 2 （循环数组）
 */
public class NextGreaterElementsTwo {
    public static int[] nextGreaterElements(int[] nums) {
        int[] res = new int[nums.length];
        // 循环数组
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int tmp = -1;
            for (int j = (i + 1) % nums.length; j != i; j = (j + 1) % nums.length) {
                if (nums[j] > num) {
                    tmp = nums[j];
                    break;
                }
            }
            res[i] = tmp;
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = new int[] {1, 2, 1, 3, 1};
        System.out.println(Arrays.toString(nextGreaterElements(nums)));
    }
}
